package com.ytclub.subject.domain.handle;

import com.alibaba.fastjson.JSON;
import com.ytclub.subject.common.enums.IsDeletedFlagEnum;
import com.ytclub.subject.common.enums.SubjectInfoTypeEnum;
import com.ytclub.subject.domain.convert.BriefSubjectConverter;
import com.ytclub.subject.domain.entity.SubjectInfoBO;
import com.ytclub.subject.domain.entity.SubjectOptionBO;
import com.ytclub.subject.infra.basic.entity.SubjectBrief;
import com.ytclub.subject.infra.basic.service.SubjectBriefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 简答题handler
 *
 * @author: HuangQi
 * @date: Created in 21:55 2023/12/20
 * @description:
 */
@Slf4j
@Component
public class BriefTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectBriefService subjectBriefService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.BRIEF;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 简答题目的插入
        if (log.isInfoEnabled()) {
            log.info("JudgeTypeHandler.add.bo:{}", JSON.toJSONString(subjectInfoBO));
        }

        // 类型转换
        SubjectBrief subjectBrief = BriefSubjectConverter.INSTANCE.convertBoToEntity(subjectInfoBO);
        subjectBrief.setSubjectId(subjectInfoBO.getId());
        subjectBrief.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        // 数据插入
        subjectBriefService.insert(subjectBrief);
    }

    /**
     * 根据题目id查询题目答案
     *
     * @param subjectId 题目id
     * @return 题目答案
     */
    @Override
    public SubjectOptionBO query(Long subjectId) {
        // 根据简答题题目id查询题目答案
        SubjectBrief subjectBrief = new SubjectBrief();
        subjectBrief.setSubjectId(subjectId);
        SubjectBrief result = subjectBriefService.queryByCondition(subjectBrief);

        // 返回简答题题目答案
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setSubjectAnswer(result.getSubjectAnswer());
        return subjectOptionBO;
    }
}
